<template>
  <div class="tab-title-page goods_list">
    <div class="common_form_search">
      <el-form
        ref="searchForm"
        :inline="true"
        :model="searchForm"
      >
        <el-form-item label="商品名称">
          <el-input
            v-model="searchForm.goods_name"
            placeholder="请输入商品名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="供应商">
          <el-input
            v-model="searchForm.shop_name"
            placeholder="请输入供应商"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="提交时间">
          <my-date-picker ref="myDatePicker"></my-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            @click="search()"
          >查询</el-button>
          <el-button
            size="small"
            @click="reset()"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-tabs
      class="flex-1 nav_tabs"
      v-model="activeName"
      @tab-click="handleClick"
    >
      <el-tab-pane
        :label="`全部(${count.all_goods})`"
        name="1"
      ></el-tab-pane>
      <el-tab-pane
        :label="`待审核商品(${count.nocheck_count})`"
        name="3"
      ></el-tab-pane>
      <el-tab-pane
        :label="`已审核商品(${count.checked_count})`"
        name="4"
      ></el-tab-pane>
      <el-tab-pane
        :label="`已拒绝商品(${count.refuse_count})`"
        name="5"
      ></el-tab-pane>
    </el-tabs>
    <el-divider class="middle_divider"></el-divider>
    <div class="child_page">
      <div class="flex align-center table-top-btns">
        <h5 class="flex align-center">
          <svg-icon
            style="margin-right:15px"
            icon-class="list"
            :size="15"
          />数据列表
        </h5>
        <el-dropdown
          trigger="click"
          v-if="($hasMethod('#pass') || $hasMethod('#refuse')) && activeName==3"
          @command="handleCommand"
        >
          <el-button
            size="small"
            class="add-btn svg-btn"
            plain
          >
            <svg-icon
              icon-class="batch"
              :size="11"
            />批量操作
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item
              command="pass"
              v-if="$hasMethod('#pass') && (activeName == 1 || activeName == 3)"
            >通过</el-dropdown-item>
            <el-dropdown-item
              command="refuse"
              v-if="$hasMethod('#refuse') && (activeName == 1 || activeName == 3)"
            >拒绝</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        border
        v-show="tableData.length > -1"
      >
        <el-table-column
          v-if="activeName==3"
          align="center"
          type="selection"
          width="55"
        />
        <el-table-column
          align="center"
          label="商品信息"
          width="300"
        >
          <template slot-scope="scope">
            <div class="flex align-center goods_info">
              <el-image
                style="width: 60px; height: 60px"
                :src="scope.row.imgurl?$globalObj.imgBaseUrl+scope.row.imgurl:''"
              >
                <div
                  slot="error"
                  class="image-slot"
                >
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
              <div class="flex-1 text-left info">
                <p class="activity">{{scope.row.id}}
                  <span
                    class="inline-block v-middle"
                    v-for="(item, index) in scope.row.activity"
                    :key="index"
                  >
                    <el-popover
                      placement="top-start"
                      width="160"
                      trigger="hover"
                      :content="item.activity_title"
                    >
                      <img
                        v-if="item.activity_img"
                        slot="reference"
                        class="w-100 h-100"
                        :src="$globalObj.imgApiUrl+item.activity_img"
                        :alt="item.activity_title"
                      >
                    </el-popover>
                  </span>
                </p>
                <p>{{scope.row.title}}</p>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="cate_name"
          label="品类"
        />
        <el-table-column
          align="center"
          prop="update_time"
          label="提交时间"
          width="100"
        />
        <el-table-column
          align="center"
          label="供应商"
        >
          <template slot-scope="scope">
            <p class="blue-font">{{scope.row.shopname}}</p>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="状态"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.ischeck == 3 ? '': scope.row.ischeck == 4 ? 'success' : 'danger'">{{scope.row.ischeck_name}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="check_reason"
          label="原因"
        />
        <el-table-column
          fixed="right"
          align="center"
          label="操作"
          width="240"
        >
          <template slot-scope="scope">
            <el-button
              v-if="$hasMethod('#view')"
              type="primary"
              size="mini"
              @click="$router.push({name:'auditViewGoods',params:{goodsId:scope.row.id}})"
            >查看商品</el-button>
            <el-button
              v-if="$hasMethod('#pass')&&scope.row.ischeck == 3"
              type="warning"
              size="mini"
              @click="pass(scope.row.id)"
            >通过</el-button>
            <el-button
              v-if="$hasMethod('#refuse')&&scope.row.ischeck == 3"
              type="danger"
              size="mini"
              @click="refuse(scope.row.id)"
            >拒绝</el-button>
          </template>
        </el-table-column>
      </el-table>
      <custom-page
        ref="customPage"
        :total="total"
        @getList="getList"
      ></custom-page>
    </div>
    <audit-dialog
      ref="auditDialog"
      @getList="getList"
    ></audit-dialog>
  </div>
</template>

<script>
import { getGoodsCheckList, goodsCheckPass } from "@/api/audit";
import customPage from "@/components/customPage";
import auditDialog from "./components/audit";
import myDatePicker from "@/components/myDatePicker";
export default {
  components: { customPage, auditDialog, myDatePicker },
  data() {
    return {
      searchForm: {
        goods_name: "",
        shop_name: "",
        create_time: ""
      },
      activeName: '1',
      count: {
        all_goods: 0,
        nocheck_count: 0,
        checked_count: 0,
        refuse_count: 0
      },
      total: 0,
      tableData: []
    }
  },
  methods: {
    // 通过
    pass(id) {
      this.$confirm("您确定要通过审核吗？", "通过审核", {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        goodsCheckPass(id).then(res => {
          this.getList(true);
        });
      }).catch(() => { });
    },
    // 拒绝
    refuse(id) {
      this.$refs.auditDialog.goodsId = id;
      this.$refs.auditDialog.dialogType = 1;
      this.$refs.auditDialog.dialogVisible = true;
    },
    // 批量操作
    handleCommand(command) {
      const list = this.$refs.multipleTable.selection;
      if (list.length === 0) {
        this.$message({
          message: "没有勾选项",
          type: "warning",
        });
        return;
      }
      this.$refs.auditDialog.goodsList = list;
      switch (command) {
        case "pass":
          this.$refs.auditDialog.dialogType = 2;
          this.$refs.auditDialog.dialogVisible = true;
          break;
        case "refuse":
          this.$refs.auditDialog.dialogType = 3;
          this.$refs.auditDialog.dialogVisible = true;
          break;

        default:
          break;
      }
    },
    // 获取数据
    getList(flag) {
      // flag为true, 则不刷新表格
      const { page, pageSize } = this.$refs.customPage;
      if (!flag) {
        this.tableData = [];
      }
      getGoodsCheckList({
        page,
        pageSize,
        ...this.searchForm,
        check_status: this.activeName
      }).then(res => {
        this.tableData = res.data || [];
        this.total = res.total;
        for (let key in this.count) {
          this.count[key] = res.count[key];
        }
      })
    },
    // 查询
    search() {
      const pickerVal = this.$refs.myDatePicker.getVal();
      if (pickerVal !== 'error') {
        this.searchForm.create_time = pickerVal;
        this.$refs.customPage.page = 1;
        this.getList();
      }
    },
    // 重置
    reset() {
      if (this.$refs.myDatePicker) {
        this.$refs.myDatePicker.reset();
      }
      this.searchForm = {
        goods_name: "",
        shop_name: "",
        create_time: ""
      };
      this.getList();
    },
    handleClick(tab) {
      this.activeName = tab.name;
      this.$refs.customPage.page = 1;
      this.getList();
    },
  },
  mounted() {
    this.getList();
  }
}
</script>

<style lang="scss" scoped>
.goods_info {
  .info {
    margin-left: 10px;
    p {
      color: #666666;
    }
    p:first-child {
      color: #999999;
    }
  }
}
.activity {
  line-height: 24px;
  span {
    width: 20px;
    height: 20px;
    margin-left: 10px;
  }
}
.child_page {
  padding: 30px;
}
.common_form_search {
  padding-top: 15px;
  padding-left: 15px;
  padding-right: 15px;
  background-color: #ffffff;
  border-bottom: 1px solid #eeeeee;
}
</style>